package com.itserver.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itserver.mapper.sys.DictMapper;
import com.itserver.pojo.PageResult;
import com.itserver.pojo.sys.Dict;
import com.itserver.pojo.sys.QueryDict;
import com.itserver.service.sys.DictService;
import com.itserver.utils.RequestHeader;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public PageResult<Dict> list(QueryDict queryDict) {
        PageHelper.startPage(queryDict.getPage(), queryDict.getPageSize());
        List<Dict> dicts = dictMapper.list(queryDict);
        PageInfo<Dict> pageInfo = new PageInfo<>(dicts);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void add(Dict dict) {
        Claims userInfo = RequestHeader.getUserInfo();
        if (userInfo != null) {
            dict.setCreateUser((String) userInfo.get("username"));
        }
        dictMapper.add(dict);
    }

    @Override
    public void update(Dict dict) {
        Claims userInfo = RequestHeader.getUserInfo();
        if (userInfo != null) {
            dict.setUpdateUser((String) userInfo.get("username"));
        }
        dictMapper.update(dict);
    }
    @Override
    public void delete(List<Integer> ids) {
        dictMapper.delete(ids);
    }

    @Override
    public Dict getById(Integer id) {
        return dictMapper.getById(id);
    }

    @Override
    public Dict getByInfo(String type) {
        return dictMapper.getByInfo(type);
    }
}
